<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Programming in Lua : 3.2</title>
<link rel="stylesheet" type="text/css" href="pil.css">
</head>
<body>
<p class="warning">
<A HREF="contents.html"><IMG TITLE="Programming in Lua (first edition)" SRC="capa.jpg" ALT="" ALIGN="left"></A>This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.<BR>The third edition targets Lua 5.2 and is available at <A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/theprogrammil3-20">Amazon</A> and other bookstores.<BR>By buying the book, you also help to <A HREF="../donations.html">support the Lua project</A>.
</p>
<table width="100%" class="nav">
<tr>
<td width="10%" align="left"><a href="3.1.html"><img border="0" src="left.png" alt="Previous"></a></td>
<td width="80%" align="center"><a href="contents.html"><font face="Helvetica,Arial,sanserif">
<font color="gray">Programming in </font><font color="blue"> Lua</font>
</font></a></td>
<td width="10%" align="right"><a href="3.3.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
<tr>
<td width="10%" align="left"></td>
<td width="80%" align="center"><a href="contents.html#P1">Part I. The Language</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="contents.html#3">Chapter 3. Expressions</a></td>
<td width="10%" align="right"></td></tr>
</table>
<hr/>
<p><h2>3.2 &ndash; Relational Operators</h2>
Lua provides the following relational operators:
<pre>
    &lt;   >   &lt;=  >=  ==  ~=
</pre>
All these operators always result in <B>true</B> or <B>false</B>.

<p>The operator <code>==</code> tests for equality;
the operator <code>~=</code> is the negation of equality.
We can apply both operators to any two values.
If the values have different types,
Lua considers them different values.
Otherwise, Lua compares them according to their types.
Specifically, <B>nil</B> is equal only to itself.

<p>Lua compares tables, userdata, and functions by reference,
that is, two such values are considered equal only if they
are the very same object.
For instance, after the code
<pre>
    a = {}; a.x = 1; a.y = 0
    b = {}; b.x = 1; b.y = 0
    c = a
</pre>
you have that <code>a==c</code> but <code>a~=b</code>.

<p>We can apply the order operators only to two numbers or
to two strings.
Lua compares numbers in the usual way.
Lua compares strings in alphabetical order,
which follows the locale set for Lua.
For instance, with the European Latin-1 locale,
we have <code>"acai" &lt; "a&ccedil;a&iacute;" &lt; "acorde"</code>.
Other types can be compared only for equality (and inequality).

<p>When comparing values with different types,
you must be careful:
Remember that <code>"0"==0</code> is false.
Moreover, <code>2&lt;15</code> is obviously true,
but <code>"2"&lt;"15"</code> is false (alphabetical order!).
To avoid inconsistent results,
Lua raises an error when you mix strings and numbers in
an order comparison, such as <code>2&lt;"15"</code>.

<hr/>
<table width="100%" class="nav">
<tr valign="top">
<td width="90%" align="left">
<small>
  Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
</small>
</td>
<td width="10%" align="right"><a href="3.3.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
</table>


</body></html>

